package com.fwj.authorization.staff.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fwj.authorization.staff.entity.SysPermission;
import com.fwj.authorization.staff.entity.SysRole;
import com.fwj.authorization.staff.service.SysPermissionService;
import com.fwj.base.entity.R;
import com.fwj.base.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 权限控制器
 *
 * @author 樊温军
 * @date 2025/3/18 上午10:32
 */
@Slf4j
@RestController
@RequestMapping("/permission")
@Api(tags = "权限控制器")
@RequiredArgsConstructor
public class SysPermissionController {

    private final SysPermissionService sysPermissionService;

    /**
     * 权限码—分页
     *
     * @param page          分页
     * @param sysPermission 查询条件
     * @return R<IPage < SysPermission>>
     */
    @ApiOperation(value = "权限码—分页")
    @GetMapping("/page")
    public R<IPage<SysPermission>> getPage(Page<SysPermission> page, SysPermission sysPermission) {
        try {
            log.info("权限码—分页，分页对象：{}，参数：{}", JSONUtil.toJsonStr(page), JSONUtil.toJsonStr(sysPermission));
            IPage<SysPermission> iPage = sysPermissionService.getPage(page, sysPermission);
            log.info("权限码—分页，返回结果：{}", JSONUtil.toJsonStr(iPage));
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 权限码—保存
     *
     * @param sysPermissions 权限码列表
     * @return R<IPage < SysPermission>>
     */
    @ApiOperation(value = "权限码—保存")
    @PostMapping("/save")
    public R<Boolean> savePermission(@Valid @RequestBody List<SysPermission> sysPermissions) {
        try {
            log.info("权限码—保存，参数：{}", JSONUtil.toJsonStr(sysPermissions));
            Boolean flag = sysPermissionService.savePermission(sysPermissions);
            log.info("权限码—保存，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 权限码—批量删除
     *
     * @param ids 权限码ID列表
     * @return R<IPage < SysPermission>>
     */
    @ApiOperation(value = "权限码—批量删除")
    @PostMapping("/del")
    public R<Boolean> del(@Valid @RequestBody List<Long> ids) {
        try {
            log.info("权限码—批量删除，参数：{}", JSONUtil.toJsonStr(ids));
            Boolean flag = sysPermissionService.removeByIds(ids);
            log.info("权限码—批量删除，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 查询某URL的所有角色
     *
     * @return R<List < SysRole>>
     */
    @ApiOperation(value = "查询某URL的所有角色")
    @GetMapping("/roleOfUrl")
    public R<List<SysRole>> roleOfUrl(@RequestParam("url") String url) {
        try {
            List<SysRole> list = sysPermissionService.getRoleListByPermissionUrl(url);
            return R.ok(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.failed(e.getMessage());
        }
    }

}

